﻿using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SIR.Common.Condition
{
	/// <summary>
	/// Operators matching normal comparison operators
	/// </summary>
	public sealed class PredicateOperator
	{
		public static readonly PredicateOperator EqualTo = "=";
		public static readonly PredicateOperator NotEqualTo = "<>";
		public static readonly PredicateOperator LessThan = "<";
		public static readonly PredicateOperator LessThanOrEqualTo = "<=";
		public static readonly PredicateOperator GreaterThan = ">";
		public static readonly PredicateOperator GreaterThanOrEqualTo = ">=";
		public static readonly PredicateOperator Modulus = "%";
		public static readonly PredicateOperator Like = "LIKE";

		internal static readonly ReadOnlyCollection<PredicateOperator> AllOperators = new ReadOnlyCollection<PredicateOperator>(
			new List<PredicateOperator>()
			{
				EqualTo,
				NotEqualTo,
				LessThan,
				LessThanOrEqualTo,
				GreaterThan,
				GreaterThanOrEqualTo,
				Modulus,
				Like
			}
			);

		/// <summary>
		/// Create an operator with the string representation
		/// </summary>
		/// <param name="op">String representation of the operator</param>
		private PredicateOperator(string op)
		{
			_op = string.IsNullOrWhiteSpace(op) ? "=" : op;
		}

		private string _op = null;

		/// <summary>
		/// Converts a string to the operator
		/// </summary>
		/// <param name="op">String representation of an operator</param>
		/// <returns>A new predicate operator</returns>
		public static implicit operator PredicateOperator(string op)
		{
			return new PredicateOperator(op);
		}

		/// <summary>
		/// Convert the operator to a string
		/// </summary>
		/// <param name="pOperator">The operator to be converted</param>
		/// <returns>The string representation of the operator</returns>
		public static implicit operator string(PredicateOperator pOperator)
		{
			return pOperator._op;
		}

		/// <summary>
		/// Returns the hash code of the object
		/// </summary>
		/// <returns>The hash value of the object</returns>
		public override int GetHashCode()
		{
			return _op.GetHashCode();
		}

		// Returns the string representation
		public override string ToString()
		{
			return _op;
		}

		/// <summary>
		/// Compares the operator to another object to see if they are equal
		/// </summary>
		/// <param name="obj">The object for comparison</param>
		/// <returns>True of the predicate is equal to the passed object</returns>
		public override bool Equals(object obj)
		{
			return _op == obj as string;
		}
	}
}